<?php
/**
 * @Name IndexController.php
 * @Description
 * @Author chain
 * @Date 2023/3/10 11:34
 */

namespace App\Http\Controllers\Admin\DataBoard;


use App\Common\Exception\ErrorException;
use App\Common\Utils\JsonResponse;
use App\Http\Controllers\Controller;
use http\Env\Request;
use App\Http\Request\Admin\DataBoard\Index\GetDataRequest;
use Illuminate\Support\Facades\Auth;


class IndexController extends Controller
{

    public function __construct()
    {
        parent::__construct();
        $this->middleware('auth');
        $this->key = env('BOARD_KEY');//接口密钥
        $this->secret = env('BOARD_SECRET');//接口密钥
        $this->baseUrl =  env('BOARD_BASEURL');//地址
    }

    /**
     * 区管
     * @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function district()
    {
        return view('admin.databoard.quji');
    }

    /**
     * description 数据看板接口
     * @return mixed
     */
    public function getData()
    {
        try{
            $postData = [
                'rows' =>  Request('rows'),
                'name' => Request('name'),
            ];
            $result = $this->postData($postData);
            return JsonResponse::returnJson($result);
        }catch(\Exception $e){
            return JsonResponse::returnJson(['error_code' => 1,'data' => []]);
        }
    }

    public function postData($params,$type=1)
    {
        $action = $params['action'] ?? 'data_find';
        $currentTime = date('Y-m-d H:i:s');
        $sign = $this->getSign($currentTime);
        $postData = [
            'sign' => $sign,
            'app_key' => $this->key,
            'action' => $action,
            'time' => $currentTime,
            'name' => $params['name'],
        ];
        if(!empty($params['rows'])) {
            $postData['rows'] = $params['rows'];
        }
        $result = $this->httpSend($postData);
        return $result;
    }

    /**
     * 发送HTTP请求方法
     * @param array $params 请求参数
     * @param string $method 请求方法GET/POST
     * @return array $data  响应数据
     */
    function httpSend($params = '', $method = 'POST', $header = array(), $multi = false){
        $url = $this->baseUrl .'/api/report/table';
        $opts = array(
            CURLOPT_TIMEOUT    => 30,
            CURLOPT_RETURNTRANSFER => 1,
            CURLOPT_SSL_VERIFYPEER => false,
            CURLOPT_SSL_VERIFYHOST => false,
            CURLOPT_HTTPHEADER   => $header
        );
        /* 根据请求类型设置特定参数 */
        switch(strtoupper($method)){
            case 'GET':
                $opts[CURLOPT_URL] = $url;
                break;
            case 'POST':
                //判断是否传输文件
                $opts[CURLOPT_URL] = $url;
                $opts[CURLOPT_POST] = 1;
                $opts[CURLOPT_POSTFIELDS] = $params;
                break;
            default:
                throw new Exception('不支持的请求方式！');
        }
        /* 初始化并执行curl请求 */
        $ch = curl_init();
        curl_setopt_array($ch, $opts);
        $data = curl_exec($ch);
        $error = curl_error($ch);
        curl_close($ch);
        if($error) throw new Exception('请求发生错误：' . $error);
        return $data;
    }

    /**
     * @param $currentTime
     * @description 获取签名
     * @return string
     */
    public function getSign($currentTime){
        $sign = md5(md5($this->key . $currentTime) .$this->secret);
        return $sign;
    }

}
